import 'package:flutter/material.dart';

class Demo8 extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new Demo8State();
  }
}

class Demo8State extends State<Demo8> {
  ScrollController _scrollController = new ScrollController(
    initialScrollOffset: 0,
    keepScrollOffset: true
  );
  PageStorageKey _scrollKey = new PageStorageKey('demo8');
  @override
  void initState() {
    super.initState();
    _scrollController.addListener(() {
      // print(_scrollController.offset);
    });
  }
  @override
  void dispose() {
    super.dispose();
    _scrollController.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        title: new Text('ScrollController'),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: () {
          _scrollController.animateTo(0, duration: Duration(milliseconds: 600), curve: Curves.easeIn);
        },
        child: new IconButton(icon: new Icon(Icons.arrow_upward, color: Colors.white,)),
      ),
      body: new ListView(
        key: _scrollKey,
        controller: _scrollController,
        children: [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17].map((i) {
          return new UnconstrainedBox(
            child: new Container(
              margin: EdgeInsets.only(top: 16.0),
              width: 300.0,
              height: 100.0,
              color: Colors.blue,
              alignment: Alignment.center,
              child: new Text(i.toString()),
            ),
          );
        }).toList(),
      ),
    );
  }
}